//
//  CYGHomeViewController.m
//  CYGHuntForCity
//
//  Created by cygarm on 16/2/20.
//  Copyright © 2016年 cygarm. All rights reserved.
//

#import "CYGHomeViewController.h"
#import "CYGHomeRcmdHeaderView.h"
#import "CYGHomeRcmdCell.h"
#import "CYGHotsViewController.h"
#import "CYGDetailViewController.h"

@interface CYGHomeViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UISegmentedControl *segCtrl;

@property (strong, nonatomic) UITableView *rcmdView;
@property (strong, nonatomic) UIImageView *nearbyView;

@property (strong, nonatomic) NSMutableArray *datas;

@end

@implementation CYGHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
	[self setupUI];
}

#pragma mark - UI Setup
- (void)setupNaviItem {
	// 设置titleView
	self.segCtrl.tintColor = CYGColor(26, 163, 146);
	// 文字
	NSMutableDictionary *attrs = [NSMutableDictionary
								    dictionary];
	attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:16];
	attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
	[self.segCtrl setTitleTextAttributes:attrs forState:UIControlStateNormal];
	[self.segCtrl setTitleTextAttributes:attrs forState:UIControlStateSelected];
	self.segCtrl.selectedSegmentIndex = 0;
}

- (void)setupUI {
	[self setupNaviItem];
	self.view.backgroundColor = [UIColor grayColor];
	
	// height-64
	self.rcmdView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-64) style:UITableViewStylePlain];
	// separatorStyle 默认有一条线
	self.rcmdView.separatorStyle = UITableViewCellSeparatorStyleNone;
	self.rcmdView.delegate = self;
	self.rcmdView.dataSource = self;
	self.rcmdView.backgroundColor = self.view.backgroundColor;
	[self.view addSubview:self.rcmdView];
	
	self.nearbyView = [[UIImageView alloc] initWithFrame:self.view.bounds];
	[self.nearbyView setImage:[UIImage imageNamed:@"wnxBG"]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
	DLog(@"%@", segue.description);
}

#pragma mark - IBAction
- (IBAction)segCtrlChange:(UISegmentedControl *)sender {	
	if (sender.selectedSegmentIndex == 0) {
		[self.view addSubview:self.rcmdView];
		[self.nearbyView removeFromSuperview];
	} else {
		[self.view addSubview:self.nearbyView];
		[self.rcmdView removeFromSuperview];
	}
}

- (void)rcmdTableHeadViewTap:(UITapGestureRecognizer *)tap {
	CYGHotsViewController *hotsVC = [[CYGHotsViewController alloc] init];
	hotsVC.barColor = tap.view.backgroundColor;
	hotsVC.barTitle = ((CYGHomeRcmdHeaderView *)tap.view).homeModel.tag_name;
	hotsVC.barSubTitle = ((CYGHomeRcmdHeaderView *)tap.view).homeModel.section_count;
	[self.navigationController pushViewController:hotsVC animated:YES];
}

#pragma mark - Table view delegate & data source
- (NSMutableArray *)datas {
	if (_datas == nil) {
		_datas = [NSMutableArray array];
		
		NSArray *tmpArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"HomeDatas" ofType:@"plist"]];
		for (NSDictionary *dict in tmpArray) {
			CYGHomeViewModel *homeModel = [CYGHomeViewModel homeModelWithDict:dict];
			[_datas addObject:homeModel];
		}
	}
	
	return _datas;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	CYGHomeViewModel *cellModel = self.datas[section];
	
	return cellModel.body.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
	CYGHomeViewModel *homeModel = self.datas[section];
	CYGHomeRcmdHeaderView *headView = [CYGHomeRcmdHeaderView headViewWithHomeModel:homeModel];
	
	UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rcmdTableHeadViewTap:)];
	[headView addGestureRecognizer:tap];
	headView.tag = section;
	
	return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
	return 60.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 210.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	CYGHomeViewModel *homeModel = self.datas[indexPath.section];
	CYGHomeRcmdCellModel *cellModel = [CYGHomeRcmdCellModel cellModelWithDict:homeModel.body[indexPath.row]];
	CYGHomeRcmdCell *cell = [CYGHomeRcmdCell cellWithTableView:tableView model:cellModel];
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	CYGDetailViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"idDetailVC"]; 
	[self.navigationController pushViewController:vc animated:YES];
}

@end
